package com.exit66.jukebox.data;

/**
 * @author andyb
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */

import java.io.File;
import java.util.ArrayList;

import com.exit66.jukebox.Options;
import com.exit66.jukebox.util.MediaFile;

public class ScanGroup extends Thread {
	
	private String		_fileSep;
	private ArrayList 	_fileList;
	private int		_libraryID;
	private String		_libraryPath;
	private boolean		_scanAll;
	private int		_threadCount;

	private static boolean running;
	
	public ScanGroup(int libraryID, boolean scanAll) {
	
		_threadCount = 0;
		Library l = new Library();
		l.setLibraryID(libraryID);
		l.fetch();
		_libraryPath = l.getLibraryPath();
		l.setScanStatus(1);
		l.save();

		try {

			while (running == true) {
				Thread.sleep(100);
			}

		}
		catch (Exception e) {
			System.err.println(e);
		}
		System.out.println("Started scanning library (id=" + libraryID + ") at " + Options.getCurrentTime());
		running = true;
		_fileList = new ArrayList();
		setName("Exit66 ScanGroup");
		_libraryID = libraryID;
		_scanAll = scanAll;
		_fileSep = System.getProperty("file.separator");			
                this.start();

	}
	
	public void run() {
		
                recurse(new File(_libraryPath));
		//for (int i=0; i < Options.getScanThreadCount(); i++) {

			StartChildThread();
			
		//}	
		
	}
	
	public synchronized void threadIsDone() {
	
		_threadCount--;
		StartChildThread();		
				
	}
	
	public synchronized void StartChildThread() {
		
		if (_fileList.size() > 0) {
			
			File f = (File)_fileList.get(0);
			ScanThread t = new ScanThread(this, f.getAbsolutePath(), _libraryID, _libraryPath, _scanAll);
			_fileList.remove(0);
			_threadCount++;
			
		}
		else {

			if (_threadCount == 0) {
				Library l = new Library();
				l.setLibraryID(_libraryID);
				l.fetch();
				l.setScanStatus(0);
				l.save();
				System.out.println("Completed scanning library (id=" + _libraryID + ") at " + Options.getCurrentTime());
                Maintenance maint = new Maintenance();
                maint.cleanDatabase();
				running = false;

			}

		}
		
	}
        
            
        private void recurse(File dir) {

            String extension;

            if ((dir != null) && (dir.isDirectory())) {

                String dirlist[] = dir.list();

                for (int i=0; i < dirlist.length; i++) {

                    File importFile = new File(dir.getPath() + _fileSep + dirlist[i]);
                    if (importFile.isDirectory()) {

                        recurse(importFile);

                    } else {

                        extension = Options.getExtension(importFile.getName()).toUpperCase();
                        if ((extension.compareTo("MP3") == 0) ||
                                (extension.compareTo("OGG") == 0))
                            _fileList.add(importFile);

                    }
                }

            }

        }

	
	public class ScanThread extends Thread {
	
		private ScanGroup 	_parent;
		private String		_fileName;
		private int			_libraryID;
		private String		_libraryPath;
		private boolean		_scanAll;
		
		public ScanThread(ScanGroup callingThread, String fileName, int libraryID, String libraryPath, boolean scanAll) {
	
			_libraryID = libraryID;
			_libraryPath = libraryPath;	
			_parent = callingThread;
			_fileName = fileName;
			_scanAll = scanAll;
			setName("Exit66 ScanThread");
			this.start();
								
		}
		
		public void run() {
		
			if (_scanAll == false) {
				Track t = new Track();
				
				t.setLibraryID(_libraryID);
				t.setFileName( _fileName);
				if (t.find()) {
					return;	
				}
			}
			MediaFile m = new MediaFile(_fileName);
			m.setLibraryID(_libraryID);
			m.setLibraryPath(_libraryPath);
			if (m.importTagInfo() == false) {

				Library l = new Library();
				l.setLibraryID(_libraryID);
				l.addFailedFile(_fileName, _fileName.substring(_libraryPath.length()));

			}
			_parent.threadIsDone();
				
		}
	}

}

